package com.qf.controller;

import com.qf.Resp.BaseResp;
import com.qf.borrower.Borrower;
import com.qf.service.UserService;
import com.qf.service.UserService2;
import com.qf.user.TbUser;
import com.qf.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author H
 * @create 2020-11-03 10:58
 * @strive To be whoever you want to be.
 * @Description:
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    UserService2 userService2;
    @Autowired
    JWTUtils jwtUtils;

    @RequestMapping("/login")
    public BaseResp login(@RequestBody Borrower borrower) {
        return userService.login(borrower);
    }

    @RequestMapping("/login2")
    public BaseResp login(@RequestBody TbUser tbUser) {
        return userService2.login2(tbUser);
    }

    @RequestMapping("/findClasses")
    public BaseResp findClasses(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String token = null;
        if (cookies != null) {
            token = getToken(cookies);
        } else {
            System.out.println("token为null");
        }


        return userService.findClasses(token);
    }


    public String getToken(Cookie[] cookies) {
        String token = null;
        for (Cookie cook : cookies
        ) {
            if (cook.getName().equals("token")) {
                token = cook.getValue();
            }
        }
        return token;
    }

}

